package com.zg.invoice.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zg.common.core.R;
import com.zg.invoice.service.InvoiceService;
import com.zg.invoice.vo.InvoicePageReq;
import com.zg.invoice.vo.JsInvoiceOpenTaskVo;
import com.zg.invoice.vo.OpenInvoiceReq;
import com.zg.invoice.vo.OpenRedInvoiceReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(tags = {"结算发票功能"})
public class ApiInvoiceController {

    @Resource
    private InvoiceService invoiceService;

    /**
     * 查询发票信息
     * @return
     */
    @ApiOperation("查看开票列表")
    @PostMapping("/invoice/page")
    public R<IPage<JsInvoiceOpenTaskVo>> invoiceList (@RequestBody @Valid InvoicePageReq invoicePageReq){
        return R.success(invoiceService.invoicePageVo(invoicePageReq));
    }
    /**
     * 开蓝票
     * @return
     */
    @ApiOperation("开具蓝票")
    @PostMapping("/invoice/issue")
    public R<String>  invoice (@RequestBody  @Validated OpenInvoiceReq openInvoiceReq){
        try{
            String result =invoiceService.invoice(openInvoiceReq);
            if (StringUtils.isEmpty(result)){
                return R.success();
            }else {
                return R.error(result);
            }
        }finally {

        }

    }
    /**
     * 开红票
     * @return
     */
    @ApiOperation("开具红票")
    @PostMapping("/invoice/red")
    public R<String>  invoiceRed (@RequestBody  @Validated  OpenRedInvoiceReq openRedInvoiceReq){
        String result =invoiceService.invoiceRed(openRedInvoiceReq);
        if (StringUtils.isEmpty(result)){
            return R.success();
        }else {
            return R.error(result);
        }
    }


}
